package com.jyy.prefabricated.service;

import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.entity.MessageInfo;
import com.jyy.prefabricated.enums.BaseStatusEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.MessageInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class MessageInfoService {

    @Autowired
    private MessageInfoMapper messageInfoMapper;

    /**
     * 新增
     * @param messageInfo
     * @return
     */
    public Integer insert(MessageInfo messageInfo){
        messageInfo.setStatus(BaseStatusEnum.INVALID.getValue());
        messageInfoMapper.insert(messageInfo);
        return messageInfo.getId();
    }

    /**
     * 更新
     * @param messageInfo
     * @return
     */
    public int update(MessageInfo messageInfo){
        if(messageInfo.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        return messageInfoMapper.updateByPrimaryKeySelective(messageInfo);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        return messageInfoMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public MessageInfo selectById(Integer id){
        return messageInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据用户id查询
     * @param userId
     * @return
     */
    public List<MessageInfo> selectByParams(Integer userId){
        return messageInfoMapper.selectByParams(userId);
    }

    /**
     * 批量更新状态
     * @param dto
     */
    public void batchUpdateStatus(BatchDto dto){
        if(dto.getIds().size() > 0){
            dto.setStatus(BaseStatusEnum.VALID.getValue());
            messageInfoMapper.batchUpdateStatus(dto.getIds(), dto.getStatus());
        }
    }

    /**
     * 批量删除
     * @param dto
     * @return
     */
    public int batchDelete(BatchDto dto){
        if(dto.getIds().size() == 0){
            throw new CustomException("参数id不能为空");
        }
        return messageInfoMapper.batchDelete(dto.getIds());
    }
}
